//
//  LoginViewController.m
//  MyVirtualDiary
//
//  Created by Alexander Wallis on 09.07.13.
//  Copyright (c) 2013 Alexander Wallis. All rights reserved.
//

#import "LoginViewController.h"
#import "NewUserViewController.h"
#import "UserViewController.h"
#import "AppDelegate.h"
#import "User.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

@synthesize userArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    /*
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addUser)];
    self.navigationItem.leftBarButtonItem = item;
    
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(login)];
    self.navigationItem.rightBarButtonItem = item2; 
    */
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private Methods

-(AppDelegate *)appDelegate{

    return (AppDelegate *) [[UIApplication sharedApplication]delegate];
    
}

-(void)addUser{

    NewUserViewController *newUserViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NewUserViewController"];
    [self.navigationController pushViewController:newUserViewController animated:YES];
    
}

-(IBAction)login:(id)sender{
    
    //check if UITextFields are empty 
    if ([[self.nameField text]length] == 0 || [[self.passwordField text]length] == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Please enter a Name and Password" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }else
        [self checkLogin]; 
}

-(void)checkLogin{

    NSManagedObjectContext *context = [[self appDelegate] managedObjectContext];
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name==%@ AND password==%@", self.nameField.text, self.passwordField.text];
    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:predicate];
    
    NSError *error = nil;
    self.userArray = [context executeFetchRequest:fetchRequest error:&error];
    
    if (userArray.lastObject == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Please enter valide Name and Password" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }else{
        User *user = [self.userArray lastObject];
        UserViewController *userViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"UserViewController"];
        UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:userViewController];
        userViewController.userID = [user objectID];
        [self presentViewController:navigationController animated:YES completion:nil];
    }
}

-(IBAction)backgroundTouched:(id)sender{

    [self.passwordField resignFirstResponder];
    [self.nameField resignFirstResponder]; 
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    if (textField == self.passwordField || textField == self.nameField) {
        [self.nameField resignFirstResponder];
        [self.passwordField resignFirstResponder];
    }
    return NO;
}
@end
